import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';

Vue.use(Vuex);
import userModule from "./modules/userModule.js"
import searchModule from "./modules/searchModule.js"
import nineModule from "./modules/nineModule.js"


var store = new Vuex.Store({
    state:{
        list:[],
        items:[],
        tripInfo:[],
        page:0,
        hotTrip:[],
        hotpage:1,
        empty:false,
        newLine:[],
        newPage:1,
        specialPage:1,
        specialTrip:[],
        country:[],
        startCity:[],
        arrInd:6,
        isShow:false,
        tripId:"",
        userInfo:{},
        islogin:false,
    },
    modules:{
        user:userModule,
        search:searchModule,
        ninePage:nineModule
    },
    mutations:{
         // 是否登录
        setIslogin:function(state,v){
            console.log(v);
            if(v.err){
                state.islogin=false;
            }else{
                state.islogin=true;
                state.userInfo=v.msg
            }
        },
        changeIslogin:function(state,v){
            // console.log(v)
            state.islogin=v;
        },
       setLoop:function(state,value){
        //    console.dir(value)
            state.list = value;
       },
       setItems:function(state,value){
           state.items = value;
        //    console.dir(state.items)
       },
        //    设置旅游信息
        setTripInfo:function(state,value){
            state.tripInfo=value.list;
            state.tripId=value._id
            // console.log(state.search.tripId)
        },
        setArrInd:function(state,value){
            // console.log(state.tripInfo.length)
            if(state.arrInd<state.tripInfo.length){
                state.arrInd+=6
                // console.log(state.arrInd)
            }else{
                state.empty=true;
            }
        },
        // 设置开元精品信息
        setGoodTrip(state,value){
            state.hotTrip = state.hotTrip.concat(value);
            // console.log(state.hotpage);
            state.hotpage = state.hotTri.length/5;
        },
        // 控制加载更多
        setempty(state,value){
            state.empty=value
        },
        // 修改请求精品页数
        resetHotpage(state){
            state.hotTrip=[]
            state.hotpage=1;
        },
        // 修改是否还有数据
        changeEmpty(state){
            state.empty=false
        },
        // 保存新品路线
        setNewLine(state,value){
            state.newLine=state.newLine.concat(value);
            state.newPage = Math.ceil(state.newLine.length/5)
        },
        // 修改新品路线页数
        changNewPage(state){
            state.newLine=[]
            state.newPage = 1
        },
        // 保存优品路线
        setSpecialTrip(state,value){
            state.specialTrip=state.specialTrip.concat(value);
            state.specialPage = Math.ceil(state.specialTrip.length/5)
        },
        // 保存城市列表
        setCountry(state,value){
            // console.dir(value);
            state.country=value
        },
        // 保存出发城市
        setStartCity(state,value){
            state.startCity = value
        }

    },
    actions:{
        // 退出登录
        logoutReuqest(state){
            axios.get("/api/users/logout")
            .then(res=>{
                console.log(res.data);
                state.commit("changeIslogin",false)
            })
        },
         // 是否登录
        requestIslogin:function(state){
            // console.log(222)
            axios.get("/api/users/islogin")
            .then(res=>{
                // console.log(res.data)
                state.commit("setIslogin",res.data)
            })  
        },
        requestLoop:function(state,data){
            axios.get("/api/loop")
            .then(res=>{
                // console.log(res.data.msg)
                state.commit("setLoop",res.data.msg)
            })
        },
        requestItems:function(state,data){
            axios.get("/api/items")
            .then(res=>{
                // console.log(res.data)
                state.commit("setItems",res.data.msg)
            })
        },
        // 请求旅游信息
        requestTripInfo:function(state,data){
            axios.post("/api/tripInfo")
            .then(res=>{
                // console.log(res.data.msg[0])
                state.commit("setTripInfo",res.data.msg[0])
            })
        },
        // 请求精品旅游
        requestGoodTrip(state,data){
            axios.get("api/goodTrip",{params:{page:state.state.newPage}})
            .then(res=>{
                state.commit("setGoodTrip",res.data.data.list)
            })
        },
        //请求更多精品旅游
        requestMoreGoodTrip(state,data){
            state.state.hotpage=state.state.hotpage+1;
            axios.get("api/goodTrip",{params:{page:state.state.hotpage}})
            .then(res=>{
                // console.dir(res.data.data.list)
                if(res.data.data.list){
                    state.commit("setGoodTrip",res.data.data.list)
                }else{
                    state.commit("setempty",true)
                }            
            });
        },
        // 请求新品路线
        requestNewLine(state){
            axios.get("api/newLine",{params:{page:state.state.newPage}})
            .then(res=>{
                if(res.data.data.list){
                    state.commit("setNewLine",res.data.data.list);
                }
            });
        },
        // 请求更多新品路线
        requestMoreNewLine(state){
            state.state.newPage=state.state.newPage+1
            axios.get("api/newLine",{params:{page:state.state.newPage}})
            .then(res=>{
                if(res.data.data.list){
                    state.commit("setNewLine",res.data.data.list);
                }else{
                    state.commit("setempty",true)
                }
            })
        },
        // 请求优品特色
        requestSpecialTrip(state){
            axios.get("api/specialTrip",{params:{page:state.state.specialPage}})
            .then(res=>{
                if(res.data.data.list){
                    state.commit("setSpecialTrip",res.data.data.list);
                }
            });
        },
        // 请求更多优品特色
        requestMorespecialTrip(state){
            state.state.specialPage=state.state.specialPage+1
            axios.get("api/specialTrip",{params:{page:state.state.specialPage}})
            .then(res=>{
                if(res.data.data.list){
                    state.commit("setSpecialTrip",res.data.data.list);
                }else{
                    state.commit("setempty",true)
                }
            });
        },
        // 请求国家
        requestCountry(state){
            axios.get("api/search")
            .then(res=>{
                // console.dir(res.data.msg)
                state.commit("setCountry",res.data.msg)
            })
        } , 
        // 请求出发城市
        requestStartCity(state){
            axios.get("api/searchStart")
            .then(res=>{
                // console.log(res.data)
                state.commit("setStartCity",res.data.msg)
            })
        }
    }
})

export default store;